/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.documents.service.product;

import com.documents.common.core.PageBean;
import com.documents.common.core.service.BaseSrv;
import com.documents.htmlobj.EnProductObj;
import com.documents.pojo.Product;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Administrator
 */
public class ProductService extends BaseSrv<Product, Integer> {


    public PageBean getPageBean(int page, int pageSize, Product product) {
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);
        pageBean.setCurrentPage(page);
        String condition = "";
        Object[] parameters = new Object[1];
        int paramIndex = 0;
        if (product != null) {
            int pid = product.getPid();
            if (pid >= 0) {
                condition = condition + "pid=?";
                parameters[paramIndex] = new Integer(pid);
                paramIndex++;
            }
            Long allRow = this.findTotalCount(condition, parameters);
            int totalPage = PageBean.countTotalPage(pageSize, allRow.intValue()); // 总页数
            pageBean.setAllRow(allRow.intValue());
            pageBean.setTotalPage(totalPage);
            List<Product> list;
            if (page == 1) {
                list = this.findWithPaging(condition, parameters, "code", page-1, pageSize);
            } else {
                list = this.findWithPaging(condition, parameters, "code", (page - 1) * pageSize, pageSize);
            }

            pageBean.setList(list);

        } else {
            Long allRow = this.findTotalCount("", null);
            int totalPage = PageBean.countTotalPage(pageSize, allRow.intValue()); // 总页数
            pageBean.setAllRow(allRow.intValue());
            pageBean.setTotalPage(totalPage);
             List<Product> list;
            if (page == 1) {
                list = this.findWithPaging(condition, parameters, "code", page-1, pageSize);
            } else {
                list = this.findWithPaging(condition, parameters, "code", (page - 1) * pageSize, pageSize);
            }

            pageBean.setList(list);
        }
        return pageBean;
    }

    public void saveInstance(Product product) {

        Product parentGroup = this.findUnique("id=?", new Object[]{product.getPid()});
        product.setCode(this.generateCode(parentGroup));
        this.save(product);
       
    }

    /*
     * 生成编码,父级性质是2位编码，子级性质是4位编码
     */
    @SuppressWarnings("unchecked")
    private String generateCode(Product parent) {
        List<Product> groups = null;
//        ?this.findUnique("objid=?", new Object[]{obj_id});
        groups = this.dao.find("pid=?", new Object[]{parent.getId()}, "id");
        if (groups == null || groups.isEmpty()) {//不存在兄弟
            if (null == parent) {//父亲也为空的话，代表第一次添加
                return "001";
            } else {
                return parent.getCode() + "001";
            }
        }
        String code = "";
        Product max = groups.get(0);
        int maxCode = 0;
        for (Product group : groups) {
            int currCode = Integer.valueOf(group.getCode().substring(group.getCode().length() - 3, group.getCode().length()));
            if (currCode > maxCode) {
                maxCode = currCode;
            }
        }
        if (String.valueOf(max.getCode()).length() == 3) {
            code = valueOf(String.valueOf(maxCode + 1));
        } else {
            code = max.getCode().substring(0, max.getCode().length() - 3) + valueOf(String.valueOf(maxCode + 1));
        }
        return code;
    }

    private String valueOf(String str) {
        if (str == null || str.isEmpty()) {
            return "000";
        }
        try {
            int code = Integer.valueOf(str);
            if (code < 10 && code > 0) {
                return "00" + code;
            }
            if (code < 100 && code >= 10) {
                return "0" + code;
            } else {
                return String.valueOf(code);
            }
        } catch (Exception e) {
            return "000";
        }
    }

    public List<Product> getPropertyList() {
        String condition = "level=?";
        Object[] parameters = new Object[1];
        parameters[0] = 2;
        List<Product> list = this.find(condition, parameters, "id");
        return list;

    }

    public void deleteById(Integer documentGroupId) {
        Product documentGroup = null;
        documentGroup = this.findUnique("id=?", new Object[]{documentGroupId});

        if (documentGroup != null) {
            String code = documentGroup.getCode();
            String cString = code + "%";

            List<Product> list = this.find("code like ?", new Object[]{cString}, "id");


            for (Product d : list) {
                this.delete(d);
            }
        }
    }

    public HashMap<String, List<EnProductObj>> createEn() {
        HashMap map = new HashMap<String, List<EnProductObj>>();

        List<Product> pl = this.findAll();
        for (Product p : pl) {
            String key = p.getEn();
            if(key==null||key.equals(""))continue;
            if (!map.containsKey(key)) {
                List<EnProductObj> list = new ArrayList<EnProductObj>();
                map.put(key, list);
            }
            List<EnProductObj> tmplist = (List<EnProductObj>) map.get(key);

            EnProductObj o = new EnProductObj();
            o.setId(p.getId());
            o.setName(p.getName());
            tmplist.add(o);
            map.put(key, tmplist);

        }
        return map;
    }
}
